<?php

namespace app\api\controller;

use app\data\Data;
use app\api\model\News as news_list;
use think\Db;
use think\Request;
use app\api\model\Form;
use app\api\model\Form as order_list;
use think\Cookie;
use app\api\model\Preg;

class Shop extends Index
{


    //店铺类型
    public function get_category()
    {
        $res = db('shop_category')->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '成功', $res);

        } else {
            $this->_jsonp('1000', '失败', null);
        }
    }

    /**
     * 显示资源列表
     *店铺审核信息管理
     * @return \think\Response
     */

    public function shop_audits()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();


        $where['shop_id'] = (isset($_GET['shop_category_id']) && !empty($_GET['shop_category_id'])) ? $_GET['shop_category_id'] : array('neq', 0);
        $where['status'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
        $where['time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['name|phone|email'] = array('like', "%$username%");

        $in[0] = '__SHOP_CATEGORY__ s';
        $in[1] = 's.id = f.shop_id';
        $alias = 'f';
        $field = 'f.*,s.shop_name';

        $zgh_shop_audits = new Data('shop_information');
        $res['data'] = $zgh_shop_audits->joinPage($page, $in, $alias, $where, $field);
        $res['count'] = $zgh_shop_audits->count($where);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '获取店铺审核信息成功', $res);
        } else {
            $this->_jsonp('1000', '获取店铺审核信息失败', null);
        }
    }

    /**
     * 店铺审核信息管理
     * 详情页
     */
    public function shop_msg1()
    {
        $id = trim(input('get.id'));
        $where['f.id'] = $id;
        $in[0] = '__SHOP_CATEGORY__ s';
        $in[1] = 's.id = f.shop_id';
        $alias = 'f';
        $field = 'f.*,s.shop_name';
        $zgh_shop_audits = new Data('shop_information');
        $res = $zgh_shop_audits->joinOne($in, $where, $field, $alias);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }

    }

    /**
     * 店铺审核信息管理
     * 修改状态
     * 通过
     */
    public function shop_audits_pass()
    {
        $where['id'] = trim(input('get.id'));
        $data['status'] = 2;
        $data['static'] = 1;
        $zgh_shop_audits = new Data('shop_information');
        $res = $zgh_shop_audits->_update($data, $where);
        if (isset($res) || !empty($res)) {
            action_log('店铺修改状态', 'log_admin', 1);
            $this->_jsonp('200', '修改成功', $res);
        } else {
            action_log('店铺修改状态', 'log_admin');
            $this->_jsonp('1000', '修改失败', null);
        }
    }

    /**
     * 店铺审核信息管理
     * 修改状态
     * 不通过
     */
    public function shop_audits_nopass()
    {
        $where['id'] = trim(input('get.id'));
        $data['status'] = 3;
        $data['result'] = (isset($_GET['result']) && $_GET['result']) ? trim($_GET['result']) : '';
        $zgh_shop_audits = new Data('shop_information');
        $res = $zgh_shop_audits->_update($data, $where);
        if (isset($res) || !empty($res)) {
            action_log('店铺修改状态', 'log_admin', 1);
            $this->_jsonp('200', '修改成功', $res);
        } else {
            action_log('店铺修改状态', 'log_admin');
            $this->_jsonp('1000', '修改失败', null);
        }
    }


    /**
     * 显示资源列表
     *店铺信息管理
     * @return \think\Response
     */
    public function shop_information()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();


        $where['shop_id'] = (isset($_GET['shop_category_id']) && !empty($_GET['shop_category_id'])) ? $_GET['shop_category_id'] : array('neq', 0);
        $where['static'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
        $where['time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['name|phone|email'] = array('like', "%$username%");
        $where['status'] = 2;

        $join[0] = '__SHOP_CATEGORY__ s';
        $join[1] = 's.id = a.shop_id';
        $alias = 'a';
        $field = 'a.*,s.shop_name';

//        $res['data'] = db('shop_information')->where($where)->alias('a')->field($field)->page($page)->join($join[0], $join[1])->select();
//        $res['count'] = db('shop_information')->where($where)->alias('a')->count();
        $zgh_shop_information = new Data('shop_information');
        $res['data'] = $zgh_shop_information->joinPage($page, $join, $alias, $where, $field);
        $res['count'] = $zgh_shop_information->count($where);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '获取店铺信息成功', $res);
        } else {
            $this->_jsonp('1000', '获取店铺信息失败', null);
        }
    }

    /**
     *店铺信息管理
     * 查看详情
     */
    public function shop_msg2()
    {
        $id = trim(input('get.id'));
        $where['f.id'] = $id;
        $join[0] = '__SHOP_CATEGORY__ s';
        $join[1] = 's.id = f.shop_id';
        $alias = 'f';
        $field = 'f.*,s.shop_name';
        $zgh_shop_information = new Data('shop_information');
        $res = $zgh_shop_information->joinOne($join, $where, $field, $alias);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }


    /**
     * 店铺信息管理
     * 停用
     */
    public function shop_stop()
    {
        $where['id'] = trim(input('get.id'));
        $data['static'] = 2;
        $data['result'] = trim(input('get.result'));
        if (isset($where) || !empty($where)) {
            $zgh_member = new Data('shop_information');
            $zgh_member->_update($data, $where);
            action_log('店铺修改状态', 'log_admin', 1);
            $this->_jsonp('200', '操作成功', null);
        } else {
            action_log('店铺修改状态', 'log_admin');
            $this->_jsonp('1000', '操作失败', null);
        }
    }

    /**
     * 店铺信息管理
     * 启用
     */
    public function shop_start()
    {
        $where['id'] = trim(input('get.id'));
        $data['static'] = 1;
        $data['result'] = '';
        if (isset($where) || !empty($where)) {
            $zgh_member = new Data('shop_information');
            $zgh_member->_update($data, $where);
            action_log('店铺修改状态', 'log_admin');
            $this->_jsonp('200', '操作成功', null);
        } else {
            action_log('店铺修改状态', 'log_admin');
            $this->_jsonp('1000', '操作失败', null);
        }
    }

    /**
     * 前端店铺首页
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $where['uid'] = trim(input('get.uid'));
        $where['status'] = 2;
        $join[0] = "__SHOP_CATEGORY__ c";
        $join[1] = "c.id = a.shop_id";
        $field = "a.*,c.shop_name";
        $res = db('shop_information')->alias('a')->field($field)->join($join[0], $join[1])->where($where)->find();
        if (isset($res) || !empty($res)) {
			if ($res['static'] == 2) {
                $this->_jsonp('1002', '店铺被禁用', null);
            }
            if ($res['image'] == null || $res['name'] == null) {
                $this->_jsonp('1001', '资料未完善', null);
            }
            $res['label_shop'] = db('shop_label')->select();

            $where1['name'] = $res['id'];
            $where1['is_show'] = 1;
            $where1['status'] = 1;
            $where1['static'] = 2;
            $res['shop_count'] = db('market_trading')->where($where1)->count();

            $where2['name'] = $res['id'];
            $where2['status'] = 2;
			$where2['static'] = array('in',array(1,2));
            $where2['is_show'] = 1;
            $res['trading_count'] = db('market_trading')->where($where2)->count();

            $where3['s_id'] = $res['id'];
            $where3['state'] = 1;
            $where3['status'] = 1;
            $res['service_count'] = db('service_market')->where($where3)->count();

            $where4['s_id'] = $res['id'];
            $where4['state'] = 2;
            $where4['status'] = 2;
            $res['s_count'] = db('service_market')->where($where4)->count();

            $where5['seller'] = $res['uid'];
            $where5['status'] = 1;
            $where5['services'] = 1;
            $res['status1'] = db('orders')->where($where5)->count();

            $where6['seller'] = $res['uid'];
            $where6['status'] = 2;
            $where6['services'] = 1;
            $res['status2'] = db('orders')->where($where6)->count();

            $where7['seller'] = $res['uid'];
            $where7['services'] = 1;
            $where7['status'] = array('in', array(3,4,5,6,7,8));
            $res['status3'] = db('orders')->where($where7)->count();

            $where9['seller'] = $res['uid'];
            $where9['status'] = 9;
            $where9['services'] = 1;
            $res['status5'] = db('orders')->where($where9)->count();

            $where10['seller'] = $res['uid'];
            $where10['services'] = 1;
            $where10['status'] = array('in', array(3, 4, 5, 6, 7, 8, 9));
            $res['status6'] = db('orders')->where($where10)->count();

            $where11['seller'] = $res['uid'];
            $where11['services'] = 1;
            $where11['status'] = array('in', array(3, 4, 5, 6, 7, 8, 9));
            $res['status7'] = db('orders')->where($where11)->sum('money');

            $where12['seller'] = $res['uid'];
            $where12['status'] = 1;
            $where12['services'] = 2;
            $res['static1'] = db('orders')->where($where12)->count();

            $where13['seller'] = $res['uid'];
			$where13['services'] = 2;
            $where13['status'] = array('in', array(2,4,5,7,9));
            $res['static2'] = db('orders')->where($where13)->count();

            $where14['seller'] = $res['uid'];
            $where14['status'] = 10;
            $where14['services'] = 2;
            $res['static3'] = db('orders')->where($where14)->count();

            $where15['seller'] = $res['uid'];
            $where15['services'] = 2;
            $where15['status'] = 11;
            $res['static4'] = db('orders')->where($where15)->count();

            $where16['seller'] = $res['uid'];
            $where16['services'] = 2;
            $where16['status'] = 12;
            $res['static5'] = db('orders')->where($where16)->count();

            $where17['seller'] = $res['uid'];
            $where17['services'] = 2;
            $where17['statuc'] = array('in', array(2,3,4,5,6,7,8,9));
            $res['static6'] = db('orders')->where($where17)->count();

            $where18['seller'] = $res['uid'];
            $where18['services'] = 2;
            $where18['statuc'] = array('in', array(2,3,4,5,6,7,8,9));
            $res['static7'] = db('orders')->where($where18)->sum('money');


            if (isset($res) || !empty($res)) {
                $this->_jsonp('200', '查询成功', $res);
            } else {
                $this->_jsonp('1000', '您还没有开通店铺', null);
            }
        } else {
            $where19['uid'] = trim(input('get.uid'));
            $res['enterprise'] = db('authentication_enterprise')->where($where19)->find();
            $res['personal'] = db('authentication_personal')->where($where19)->find();
            $this->_jsonp('1000', '您还没有开通店铺', $res);
        }
    }

    /**
     *店铺资料查询
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function install()
    {
        $where['a.uid'] = trim(input('get.uid'));
        $join[0] = "__SHOP_CATEGORY__ s";
        $join[1] = "s.id = a.shop_id";
        $field = "a.*,s.shop_name";
        $res = db('shop_information')->alias('a')->field($field)->join($join[0], $join[1])->where($where)->find();
        $res['label_shop'] = db('shop_label')->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 店铺资料设置
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function install_add()
    {
        $news_list = new news_list();
        $files = request()->file('img');
        $image = trim(input('post.image'));
        if (!empty($files)) {
			 // 
            $match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['img']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
            //
            $img = $news_list->upload($files, 'Images/Information');
            $data['image'] = $img;
        } else if (!empty($image)) {
            $data['image'] = $image;
        } else {
            $data['image'] = "store.jpg";
        }
        $where['id'] = trim(input('post.id'));

        $data['name'] = trim(input('post.name'));
        $data['scope'] = trim(input('post.scope'));
        $data['tenet'] = trim(input('post.tenet'));
        $data['label'] = trim(input('post.label'));
		
        if (empty($where['id'])) {
			$validate = validate('Shop');

			//进行数据验证
			if (!$validate->scene('add')->check($data)) {
				$this->json('1000',$validate->getError(),null);
			}
            $res = db('shop_information')->where($where)->insert($data);
        } else {
           $info = db('shop_information')->where($where)->find();
            if ($info['name'] == $data['name']) {
                $res = db('shop_information')->where($where)->update($data);
            } else {
                $shop_info = db('shop_information')->where('id', '<>', $where['id'])->select();
                foreach ($shop_info as $key => $value) {
                    if ($value['name'] == $data['name']) {
                        $this->json('1000', '店铺名称已存在', null);
                        break;
                    }
                }
                $res = db('shop_information')->where($where)->update($data);
            }
        }
        if ($res == true) {
            action_log_index('店铺中心修改资料', 'log_ment', 1);
            $this->json('200', '修改成功', $res);
        } else {
            action_log_index('店铺中心修改资料', 'log_ment');
            $this->json('1000', '修改失败', null);
        }
    }

    /**
     * 交易分类
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function category()
    {
        $id = trim(input('get.id'));
        $res = db('market_trading_category')->where('pid', $id)->select();
        $this->_jsonp('200', 'OK', $res);
    }

    public function material()
    {
        $res = db('material')->where('static', 1)->select();
        $this->_jsonp('200', 'OK', $res);
    }
	  /**
     * curl查询专利
     */
    public function order_name()
    {
        $order = trim(input('get.order'));
        if ($order) {
            $res = db('patent')->where('order', $order)->value('title');
            if ($res) {
                $this->_jsonp('200', '查询成功', $res);
            } else {
                $res = $this->curl_i($order);
                if ($res['charge']) {
                    $data1['order'] = $res['result']['patent']['id'];
                    $data1['title'] = $res['result']['patent']['title'];
                    $data1['summary'] = $res['result']['patent']['summary'];
                    $data1['ipc'] = $res['result']['patent']['ipc'];
                    $data1['applicant'] = $res['result']['patent']['applicant'];
                    $data1['assignee'] = $res['result']['patent']['assignee'];
                    $data1['inventor'] = $res['result']['patent']['inventor'];
                    $data1['applicationDate'] = $res['result']['patent']['applicationDate'];
                    $data1['applicationNumber'] = $res['result']['patent']['applicationNumber'];
                    $data1['applicantAddress'] = $res['result']['patent']['applicantAddress'];
                    $data1['type'] = $res['result']['patent']['type'];
                    $data1['agency'] = $res['result']['patent']['agency'];
                    $data1['agent'] = $res['result']['patent']['agent'];
                    $data1['legalStatus'] = $res['result']['patent']['legalStatus'];
                    $data1['priorityNumber'] = $res['result']['patent']['priorityNumber'];
                    $data1['priorityDate'] = $res['result']['patent']['priorityDate'];
                    $data1['imagePath'] = $res['result']['patent']['imagePath'];
                    $data1['claims'] = $res['result']['patent']['claims'];
                    $data1['mainIpc'] = $res['result']['patent']['mainIpc'];
                    $data1['firstInventor'] = $res['result']['patent']['firstInventor'];
                    $data1['firstApplicant'] = $res['result']['patent']['firstApplicant'];
                    $data1['pdfList'] = $res['result']['patent']['pdfList'];
                    $data1['documentNumber'] = $res['result']['patent']['documentNumber'];
                    $data1['documentDate'] = $res['result']['patent']['documentDate'];
                    $data1['documentType'] = $res['result']['patent']['documentType'];
                    $data1['description'] = $res['result']['patent']['description'];
                    $name = $this->curl($data1['applicationNumber']);
//                    dump($name);return;
                    if ($name[0]['PatInfo']) {
                        $data2['order'] = $data1['order'];
                        $data2['SQH'] = $name[0]['PatInfo'][0]['SQH'];
                        $data2['ZLLX'] = $name[0]['PatInfo'][0]['ZLLX'];
                        $data2['FMMC'] = $name[0]['PatInfo'][0]['FMMC'];
                        $data2['ZSIPC'] = $name[0]['PatInfo'][0]['ZSIPC'];
                        $data2['ZIPC_TECH'] = $name[0]['PatInfo'][0]['ZIPC_TECH'];
                        $data2['SQR'] = $name[0]['PatInfo'][0]['SQR'];
                        $data2['GGRQ'] = $name[0]['PatInfo'][0]['GGRQ'];
                        $data2['P1'] = $name[0]['PatInfo'][0]['P1'];
                        $data2['P2'] = $name[0]['PatInfo'][0]['P2'];
                        $data2['P3'] = $name[0]['PatInfo'][0]['P3'];
                        $data2['P4'] = $name[0]['PatInfo'][0]['P4'];
                        $data2['P5'] = $name[0]['PatInfo'][0]['P5'];
                        $data2['AVG_P'] = $name[0]['PatInfo'][0]['AVG_P'];
                        $data2['total'] = $data2['P1'] + $data2['P2'] + $data2['P3'] + $data2['P4'] + $data2['P5'] + $data2['AVG_P'];
                    }
                    $title = $this->curl_e($data1['applicationNumber']);
                    if ($title[0]['PatInfo']) {
                        $data3['order'] = $order;
                        $data3['FMMC'] = $title[0]['PatInfo'][0]['FMMC'];
                        $data3['MATCHTYPE'] = $title[0]['PatInfo'][0]['MATCHTYPE'];
                        $data3['IND_FIELD'] = $title[0]['PatInfo'][0]['IND_FIELD'];
                        $data3['TOTAL_COUNT'] = $title[0]['PatInfo'][0]['TOTAL_COUNT'];
                        $data3['RES'] = json_encode($title[0]['PatInfo'][0]['RES'], true);
                    }

                    if ($res['charge'] && $name[0]['PatInfo'] && $title[0]['PatInfo']) {
                        $result = 0;
                        Db::startTrans();
                        try {
                            Db::name('patent')->insert($data1);
                            Db::name('patent_a')->insert($data2);
                            Db::name('patent_b')->insert($data3);
                            Db::commit();
                            $result = 1;
                        } catch (\Exception $e) {
                            Db::rollback();
                        }
                    }
                    if ($result) {
                        $this->_jsonp('200', '查询成功', $data1['title']);
                    } else {
                        $this->_jsonp('1001', '没有此专利', null);
                    }

                } else {
                    $this->_jsonp('1001', '没有此专利', null);
                }
            }
        } else {
            $this->_jsonp('1000', '请输入专利号', null);
        }
    }

    /**
     * 专利匹配
     * @param $order
     * @return mixed
     */
    public function curl_e($order)
    {
        $url = "http://www.91ipr.com/patentapi?sqh=$order&matchtype=3&gs=34&qatoken=47b928fa6eca082ab6666e00ad0630e74eee3d4382c52afbf2f65f0d9d1056a4&queryhead=PATMATCH";
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行命令
        $data = curl_exec($curl);
        //echo curl_getinfo($curl,CURLINFO_HTTP_CODE); //输出请求状态码
        $result = curl_exec($curl);
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $rheader = substr($result, 0, $header_size);
        $rbody = substr($result, $header_size);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        $b = rtrim($rbody, ")");
        $c = ltrim($b, "(");
        $out_put = json_decode($c, true);
        return $out_put;
    }

    /**
     * 专利
     * @param $order
     */
    private function curl_i($order)
    {
        $url = "https://way.jd.com/patenthub/base?appkey=c4bf2695b2340fdb8c61fabb440fe364&id=$order&v=1";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $data = curl_exec($curl);
        curl_close($curl);
        $res = json_decode($data, true);
        return $res;
    }


    /**
     * 专利评分
     * @param $order
     * @return mixed
     */
    private function curl($order)
    {
        $url = "http://www.91ipr.com/patentapi?sqh=$order&qatoken=47b928fa6eca082ab6666e00ad0630e74eee3d4382c52afbf2f65f0d9d1056a4&queryhead=FMQA";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $out_put = curl_exec($curl);
        curl_close($curl);
        $b = rtrim($out_put, ")");
        $c = ltrim($b, "(");
        $out_put = json_decode($c, true);
        return $out_put;
    }


    /**
     * 添加商品
     */
    public function publish()
    {
        $uid = trim(input('post.user'));
        $info = db('shop_information')->where('uid', $uid)->value('id');
        $show = trim(input('post.show'));
        if ($show == 1) {
            $news_list = new news_list();
            $files = request()->file('pic');
            if (!empty($files)) {
				 // 
				$match = new Preg();
				$token=Cookie::get('access_token','think_');
				if(empty($token)){
					$info= $match->get_tooken();
					$arrs=json_decode($info, true);
					$token=$arrs["access_token"];
					Cookie::set('access_token',$token,1728000);//使用think\Cookie
				}
				$res=$match->get_img($token,$_FILES['pic']['tmp_name']);
				$results=json_decode($res, true);
				if($results['conclusion']!='合规'){
					$this->json('1000', '图片不合规范', null);
				}
				//
                $img = $news_list->upload($files, 'Images/tradingMarket');
                $data['pic'] = $img;
            }

            $file = request()->file('image');
            if (!empty($file)) {
				 // 
            $match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['image']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
            //
                $imgs = $news_list->upload($file, 'Images/tradingMarket');
                $data['image'] = $imgs;
            } else {
                $data['image'] = trim(input('post.images'));
            }
            $data['time'] = time();
            $data['order'] = trim(input('post.order'));
            $data['username'] = trim(input('post.username'));
            $data['p_id'] = trim(input('post.p_id'));
            $data['category_id'] = trim(input('post.category_id'));
            $data['c_id'] = trim(input('post.c_id'));
            $data['update'] = trim(input('post.update'));
            $data['store'] = trim(input('post.store'));
            $data['volist'] = trim(input('post.value'));
            $data['content'] = trim(input('post.content'));
            $data['name'] = $info;
            $data['static'] = 2;
            if ($data['update'] == 1) {
                $data['be_time'] = strtotime('+1 month');
            } else if ($data['update'] == 2) {
                $data['be_time'] = strtotime('+3 month');
            } else if ($data['update'] == 3) {
                $data['be_time'] = strtotime('+6 month');
            } else if ($data['update'] == 4) {
                $data['be_time'] = strtotime('+1 year');
            }
			$validate = validate('Shop');

            //进行数据验证
            if (!$validate->scene('publish')->check($data)) {
                $this->json('1000',$validate->getError(),null);
            }
			$where['order'] = $data['order'];
            $where['title'] = $data['username'];
            $name = db('patent')->where($where)->count();
            if ($name) {
                $res = db('market_trading')->insert($data);
            } else {
                $this->json('1000', '专利号或专利名称不匹配', null);
                return false;
            }

        } else {
            $news_list = new news_list();
            $files = request()->file('pic');
            if (!empty($files)) {
				 // 
            $match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['pic']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
            //
                $img = $news_list->upload($files, 'Images/serviceGoods');
                $data['pic'] = $img;
            }
            $data['s_id'] = $info;
            $data['goods_name'] = trim(input('post.goods_name'));
            $data['classify'] = trim(input('post.classify'));
            $data['type'] = trim(input('post.type'));
            $data['price'] = trim(input('post.price'));
            $data['content'] = trim(input('post.content'));
            $data['add_time'] = time();
            //$data['status'] = 1;
			$data['edit_time'] = time();
            $res = db('service_market')->insert($data);
        }
        if (isset($res) || !empty($res)) {
            action_log_index('店铺中心新增商品', 'log_ment', 1);
            $this->json('200', '商品新增成功', $res);
        } else {
            action_log_index('店铺中心新增商品', 'log_ment');
            $this->json('1000', '商品新增失败', null);
        }

    }

    /**
     * 商品管理
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function noshelves()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $uid = trim(input('get.uid'));
        $res['info'] = db('shop_information')->where('uid', $uid)->find();
        $she = trim(input('get.she'));
        if ($she == 0) {
            $join[0] = "__MARKET_TRADING_CATEGORY__ m";
            $join[1] = "a.category_id = m.id";
            $join1[0] = "__MARKET_TRADING_CID__ c";
            $join1[1] = "c.id = a.c_id";
            $filed = "a.*,m.market_name,c.trading_name";
            $where['a.status'] = (isset($_GET['status']) && !empty($_GET['status'])) ? $_GET['status'] : array('neq', 0);
            $where['a.is_show'] = 1;
            $where['a.static'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
            $where['a.name'] = $res['info']['id'];
            $res['trading'] = db('market_trading')->alias('a')->order('a.time desc')->join($join[0], $join[1])->join($join1[0], $join1[1])->limit(3)->page($page)
                ->field($filed)->where($where)->select();
//            foreach ($res['trading'] as $key => $value) {
//                $category = db('market_trading_category')->where('id', $value['category_id'])->value('pid');
//                $res['trading'][$key]['category'] = db('market_trading_category')->where('id', $category)->value('market_name');
//                $res['trading_count'] = $key + 1;
//            }
            $res['count'] = db('market_trading')->alias('a')->join($join[0], $join[1])->join($join1[0], $join1[1])->where($where)->where('name', $res['info']['id'])->count();
            //分页
            $Page = new \app\api\model\Page($res['count'], 3);
            $render = $Page->render;

            $res['render'] = $render;
        } else {
            $join[0] = "__SERVICE_TYPE__ s";
            $join[1] = "c.classify = s.id";
            $filed = "c.*,s.t_name";
            $where['c.status'] = (isset($_GET['status']) && !empty($_GET['status'])) ? $_GET['status'] : array('neq', 0);
            $where['c.state'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
            $where['c.s_id'] = $res['info']['id'];
            $res['service'] = db('service_market')->alias('c')->field($filed)->join($join[0], $join[1])
                ->limit(3)->page($page)->where($where)->select();
            foreach ($res['service'] as $key => $value) {
                $category = db('service_type')->where('id', $value['type'])->value('pid');
                $res['service'][$key]['category'] = db('service_type')->where('id', $category)->value('t_name');
                $res['service_count'] = $key + 1;
            }
            $res['service']['count'] = db('service_market')->alias('c')->where($where)->count();
//        //分页
            $Page = new \app\api\model\Page($res['service']['count'], 3);
            $render = $Page->render;

            $res['service']['render'] = $render;
        }


        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '商品查询成功', $res);
        } else {
            $this->_jsonp('1000', '商品查询失败', null);
        }
    }


    /**
     * 修改状态
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function goods()
    {
        $where['id'] = trim(input('get.id'));
        $all = trim(input('get.all'));
        if ($all == 1) {
            $data['status'] = trim(input('get.status'));
            $data['state'] = trim(input('get.static'));
            $res = db('service_market')->where($where)->update($data);
        } else {
            $data['status'] = trim(input('get.status'));
            $data['static'] = trim(input('get.static'));

            $res = db('market_trading')->where($where)->update($data);
        }
        if (isset($res) || !empty($res)) {
            action_log_index('店铺中心修改商品状态', 'log_ment', 1);
            $this->_jsonp('200', '修改成功', null);
        } else {
            action_log_index('店铺中心修改商品状态', 'log_ment');
            $this->_jsonp('1000', '修改失败', null);
        }
    }

    /**
     * 删除
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del_goods()
    {
        $where['id'] = trim(input('get.id'));
        $num = trim(input('get.num'));
        if ($num != 1) {
            $data['is_show'] = 2;
            $res = db('market_trading')->where($where)->update($data);
        } else {
            $data['state'] = 3;
            $res = db('service_market')->where($where)->update($data);
        }
        if (isset($res) || !empty($res)) {
            action_log_index('删除店铺中心商品', 'log_ment', 1);
            $this->_jsonp('200', '好', $res);
        } else {
            action_log_index('删除店铺中心商品', 'log_ment');
            $this->_jsonp('1000', '修改失败', null);
        }
    }


    public function publish_show()
    {
        $where['uid'] = trim(input('get.uid'));
        $res = db('shop_information')->where($where)->value('shop_id');
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '修改失败', null);
        }
    }


    /**
     * 删除确认页面
     */
    public function all_goods()
    {
        $num = trim(input('get.num'));
        if ($num == 0) {
            $where['a.id'] = trim(input('get.id'));
            $join[0] = "__MARKET_TRADING_CATEGORY__ m";
            $join[1] = "a.category_id = m.id";
            $res = db('market_trading')->alias('a')->join($join[0], $join[1])->where($where)->find();
            $category = db('market_trading_category')->where('id', $res['category_id'])->value('pid');
            $res['category'] = db('market_trading_category')->where('id', $category)->value('market_name');
        } else {

            $where['c.id'] = trim(input('get.id'));
            $join[0] = "__SERVICE_TYPE__ s";
            $join[1] = "c.classify = s.id";
            $res = db('service_market')->alias('c')->join($join[0], $join[1])->where($where)->find();
        }
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '修改成功', $res);
        } else {
            $this->_jsonp('1000', '修改失败', null);
        }
    }

    /**
     * 编辑商品查询
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function publish_goods()
    {
        $num = trim(input('get.num'));
        if ($num == 0) {
            $where['id'] = trim(input('get.id'));
            $res = db('market_trading')->where($where)->find();
            $res['category'] = db('market_trading_category')->where('id', $res['p_id'])->find();
            $res['category_all'] = db('market_trading_category')->where('pid', $res['p_id'])->select();
        } else {
            $where['id'] = trim(input('get.id'));
            $res = db('service_market')->where($where)->find();
            $res['category'] = db('service_type')->where('pid', $res['classify'])->select();
        }
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 编辑商品修改
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function publish_edit()
    {
        $uid = trim(input('post.user'));
        $info = db('shop_information')->where('uid', $uid)->value('id');
        $show = trim(input('post.show'));
        $where['id'] = trim(input('post.id'));
        if ($show == 1) {
            $news_list = new news_list();
            $files = request()->file('pic');

            if (!empty($files)) {
				 // 
				$match = new Preg();
				$token=Cookie::get('access_token','think_');
				if(empty($token)){
					$info= $match->get_tooken();
					$arrs=json_decode($info, true);
					$token=$arrs["access_token"];
					Cookie::set('access_token',$token,1728000);//使用think\Cookie
				}
				$res=$match->get_img($token,$_FILES['pic']['tmp_name']);
				$results=json_decode($res, true);
				if($results['conclusion']!='合规'){
					$this->json('1000', '图片不合规范', null);
				}
				//
                $img = $news_list->upload($files, 'Images/tradingMarket');
                $data['pic'] = $img;
            } else {
                $data['pic'] = trim(input('post.pic1'));
            }

            $file = request()->file('image');
            if (!empty($file)) {
				 // 
            $match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['image']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
            //
                $imgs = $news_list->upload($file, 'Images/tradingMarket');
                $data['image'] = $imgs;
            } else {
                $data['image'] = trim(input('post.images'));
            }

            $data['order'] = trim(input('post.order'));
            $data['username'] = trim(input('post.username'));
            $data['p_id'] = trim(input('post.p_id'));
            $data['category_id'] = trim(input('post.category_id'));
            $data['c_id'] = trim(input('post.c_id'));
            $data['update'] = trim(input('post.update'));
            $data['store'] = trim(input('post.store'));
            $data['volist'] = trim(input('post.value'));
            $data['content'] = trim(input('post.content'));
            $data['name'] = $info;
            $data['static'] = 2;
            $data['status'] = 2;
            if ($data['update'] == 1) {
                $data['be_time'] = strtotime('+1 month');
            } else if ($data['update'] == 2) {
                $data['be_time'] = strtotime('+3 month');
            } else if ($data['update'] == 3) {
                $data['be_time'] = strtotime('+6 month');
            } else if ($data['update'] == 4) {
                $data['be_time'] = strtotime('+1 year');
            }
			 if (!$data['username']) {
                $this->json('1000', '专利号或专利名称不匹配', null);
                return false;
            }
			$shop_info = db('market_trading')->where('id', '<>', $where['id'])->select();
            foreach ($shop_info as $key => $value) {
                if ($value['order'] == $data['order']) {
                    $this->json('1000', '专利号已存在', null);
                    break;
                }
            }
            $where1['order'] = $data['order'];
            $where1['title'] = $data['username'];
            $name = db('patent')->where($where1)->count();
            if ($name) {
                db('market_trading')->where($where)->update($data);
            } else {
                $this->json('1000', '专利号或专利名称不匹配', null);
                return false;
            }

        } else {
            $news_list = new news_list();
            $files = request()->file('pic');
            if (!empty($files)) {
				 // 
            $match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['pic']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
            //
                $img = $news_list->upload($files, 'Images/serviceGoods');
                $data['pic'] = $img;
            }else{
                $data['pic'] = trim(input('post.image'));
            }
            $data['s_id'] = $info;
            $data['goods_name'] = trim(input('post.goods_name'));
            $data['classify'] = trim(input('post.classify'));
            $data['type'] = trim(input('post.type'));
            $data['price'] = trim(input('post.price'));
            $data['content'] = trim(input('post.content'));
            $data['status'] = 2;$data['state'] = 2;
			$data['edit_time'] = time();
            db('service_market')->where($where)->update($data);
        }
        action_log_index('店铺中心修改商品', 'log_ment', 1);
        $this->json('200', '商品修改成功', null);
    }


    /**
     * 查看我的店铺
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function center()
    {
        $index = trim(input('get.index'));
        $where['uid'] = trim(input('get.uid'));
        $where['static'] = 1;
        $where['status'] = 2;
        $join[0] = "__SHOP_CATEGORY__ c";
        $join[1] = "c.id = a.shop_id";
        $field = "a.id,a.image,a.label,a.username,a.name,a.shop_volume,c.shop_name";
        $res = db('shop_information')->alias('a')->field($field)->join($join[0], $join[1])->where($where)->find();

        $res['label_shop'] = db('shop_label')->select();


        $where2['name'] = $res['id'];
        $where2['status'] = 1;
        $where2['static'] = 2;
        $where2['is_show'] = 1;
        $res['trading_count'] = db('market_trading')->where($where2)->count();

        $where3['s_id'] = $res['id'];
        $where3['state'] = 1;
        $where3['status'] = 1;
        $res['service_count'] = db('service_market')->where($where3)->count();


        if ($index == 1) {
            $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;

            $minmoney = (isset($_GET['minmoney']) && $_GET['minmoney']) ? $_GET['minmoney'] : 0;
            $maxmoney = (isset($_GET['maxmoney']) && $_GET['maxmoney']) ? $_GET['maxmoney'] : 0;

            if ($maxmoney) {
                $where4['a.store'] = array(array('egt', $minmoney), array('elt', $maxmoney), 'and');
            } else {
                $where4['a.store'] = array('egt', $minmoney);
            }

            $where4['a.static'] = 2;
            $where4['a.status'] = 1;
            $where4['a.is_show'] = 1;
            $where4['c.static'] = 1;
            $where4['c.status'] = 2;
            $where4['a.name'] = $res['id'];

            $value = trim(input('value'));

            if ($value == 1 || $value == '1') {
                $order = 'value desc';
            } else if ($value == 2 || $value == '2') {
                $order = 'time desc';
            } else if ($value == 4 || $value == '4') {
                $order = 'store';
            } else if ($value == 5 || $value == '5') {
                $order = 'store desc';
            } else {
                $order = 'id';
            }

            $join1[0] = "__SHOP_INFORMATION__ c";
            $join1[1] = 'a.name = c.id';
            $field = "a.id,a.value,a.store,a.username,a.time,a.image,c.name";
            $alias = 'a';
            $res['trading'] = db('market_trading')->where($where4)->alias($alias)->field($field)->join($join1[0], $join1[1])->order($order)->limit(20)->page($page)->select();
//        dump(db('market_trading')->getLastSql());
            $res['count'] = db('market_trading')->where($where4)->alias($alias)->field($field)->join($join1[0], $join1[1])->count();

            //分页
            $Page = new \app\api\model\Page($res['count'], 20);
            $render = $Page->render;

            $res['render'] = $render;
        } else {

            $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
            $minmoney = (isset($_GET['minmoney']) && $_GET['minmoney']) ? $_GET['minmoney'] : 0;
            $maxmoney = (isset($_GET['maxmoney']) && $_GET['maxmoney']) ? $_GET['maxmoney'] : 0;

            if ($maxmoney) {
                $where5['w.price'] = array(array('egt', $minmoney), array('elt', $maxmoney), 'and');
            } else {
                $where5['w.price'] = array('egt', $minmoney);
            }

            $value = trim(input('value'));
            if ($value == 1 || $value == '1') {
                $order = 'value desc';
            } else if ($value == 2 || $value == '2') {
                $order = 'rate desc';
            } else if ($value == 3 || $value == '3') {
                $order = 'time desc';
            } else if ($value == 4 || $value == '4') {
                $order = 'store';
            } else if ($value == 5 || $value == '5') {
                $order = 'store desc';
            } else {
                $order = 'id';
            }
            $where5['w.state'] = 1;
            $where5['w.status'] = 1;
            $where5['i.static'] = 1;
            $where5['i.status'] = 2;
            $where5['w.s_id'] = $res['id'];

            $join2[0] = '__SHOP_INFORMATION__ i';
            $join2[1] = 'w.s_id=i.id';
            $fields = 'w.id,w.volume as value,w.rate,w.price as store,w.goods_name as username,w.add_time as time,w.pic as image,i.name';
            $res['service'] = db('service_market')->where($where5)->order($order)->limit(20)->page($page)
                ->alias('w')->field($fields)->join($join2[0], $join2[1])->select();
            $res['count'] = db('service_market')->where($where5)->alias('w')->field($fields)->join($join2[0], $join2[1])->count();
            //分页
            $Page = new \app\api\model\Page($res['count'], 20);
            $render = $Page->render;

            $res['render'] = $render;
        }
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '我的店铺查询成功', $res);
        } else {
            $this->_jsonp('1000', '我的店铺查询失败', null);
        }
    }

    /**
     * 订单管理
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function orders()
    {
        $uid = trim(input('get.uid'));
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $where['services'] = trim(input('get.index'));


        $status = (isset($_GET['status']) && !empty($_GET['status'])) ? $_GET['status'] :  array('neq',0);

        $where2['status'] = array('in', array(3,5));
        $where3['status'] = array('in', array(4,6,7,8));
        $where4['status'] = array('in', array(10,11,12,13));
		$where5['status'] = 9;

        $where['seller'] = $uid;
        $res['all_count'] = db('orders')->where($where)->count();
        $res['count1'] = db('orders')->where($where)->where('status', 1)->count();
        $res['count2'] = db('orders')->where($where)->where('status', 2)->count();
        $res['count3'] = db('orders')->where($where)->where($where2)->count();
        $res['count4'] = db('orders')->where($where)->where($where3)->count();
       // $res['count5'] = db('orders')->where($where)->where($where4)->count();
        $res['count6'] = db('orders')->where($where)->where($where5)->count();
        $res['count5'] = db('orders')->where($where)->where($where4)->count();
        if ($where['services'] == 1) {
            if ($status == 3) {
                $where1['a.status'] = array('in', array(3,5));
            } else if ($status == 4) {
                $where1['a.status'] = array('in', array(4,6,7,8));
            } else if($status == 10) {
                $where1['a.status'] = array('in',array(10,11,12,13));
            } else if($status == 9){
				$where1['a.status'] = 9;
			}else {
                $where1['a.status'] = $status;
            }
            $join[0] = "__MARKET_TRADING__ m";
            $join[1] = "m.id = a.name";
            $jo[0] = "__MARKET_TRADING_CATEGORY__ t";
            $jo[1] = "t.id = m.category_id";
            $ji[0] = "__MARKET_TRADING_CID__ c";
            $ji[1] = "c.id = m.c_id";
            $filed = "a.id,a.index,a.order,a.money,a.username as u_id,a.name as name_id,a.seller,a.status,a.s_time,a.time,m.image,m.username,m.store,t.market_name,c.trading_name";
            $res['order'] = db('orders')->order('id desc')->where($where)->alias('a')->limit(10)->page($page)
                ->field($filed)->join($join[0], $join[1])->join($jo[0], $jo[1])->join($ji[0], $ji[1])->where($where1)->select();
            foreach ($res['order'] as $key => $value) {
                if ($value['u_id'] == null) {
                    $res['order'][$key]['u_id'] = '无';
                } else {
                    $res['order'][$key]['u_id'] = db('user')->where('uid', $value['u_id'])->value('username');
                }
            }

            //分页
            if ($status == 1) {
				$res['count'] = $res['count1'];
                $Page = new \app\api\model\Page($res['count1'], 10);
            } else if ($status == 2) {
				$res['count'] = $res['count2'];
                $Page = new \app\api\model\Page($res['count2'], 10);
            } else if ($status == 3) {
				$res['count'] = $res['count3'];
                $Page = new \app\api\model\Page($res['count3'], 10);
            } else if ($status == 4) {
				$res['count'] = $res['count4'];
                $Page = new \app\api\model\Page($res['count4'], 10);
            } else if ($status == 10) {
				$res['count'] = $res['count5'];
                $Page = new \app\api\model\Page($res['count5'], 10);
            } else if ($status == 9) {
				$res['count'] = $res['count6'];
                $Page = new \app\api\model\Page($res['count6'], 10);
            } else {
				$res['count'] = $res['all_count'];
                $Page = new \app\api\model\Page($res['all_count'], 10);
            }
            $render = $Page->render;

            $res['render'] = $render;

            foreach ($res['order'] as $key => $value) {
                if ($value['s_time'] <= time()) { // 如果过了活动终止日期
                    $res['order'][$key]['shi_time'] = '0天0时0分';
                } else {

                    // 使用当前日期时间到活动截至日期时间的毫秒数来计算剩余天时分
                    $time = $value['s_time'] - time();

                    $days = 0;
                    if ($time >= 86400) { // 如果大于1天
                        $days = (int)($time / 86400);
                        $time = $time % 86400; // 计算天后剩余的毫秒数
                    }

                    $xiaoshi = 0;
                    if ($time >= 3600) { // 如果大于1小时
                        $xiaoshi = (int)($time / 3600);
                        $time = $time % 3600; // 计算小时后剩余的毫秒数
                    }

                    $fen = (int)($time / 60); // 剩下的毫秒数都算作分

                    $res['order'][$key]['shi_time'] = $days . '天' . $xiaoshi . '时' . $fen . '分';
                    $res['order'][$key]['shi_status'] = 1;
                }
            }
        } else {

        }
        $this->_jsonp('200', 'OK', $res);
    }


    public function orders_user()
    {
        $where['uid'] = trim(input('get.uid'));
        $res = db('user')->where($where)->value('username');
        $this->_jsonp('200', '获取成功', $res);
    }

    /**
     * 选择买家页面
     */
    public function orders_choose()
    {
        $value = trim(input('get.value'));
        if ($value == 1) {
            $where['a.id'] = trim(input('get.id'));
            $jo[0] = "__MARKET_TRADING_CATEGORY__ t";
            $jo[1] = "t.id = a.category_id";
            $ji[0] = "__MARKET_TRADING_CID__ c";
            $ji[1] = "c.id = a.c_id";
            $field = "a.id,a.username,a.value,a.volist,a.store,a.image,t.market_name,c.trading_name,c.id as trading_id";
            $res = db('market_trading')->where($where)->alias('a')->field($field)
                ->join($jo[0], $jo[1])->join($ji[0], $ji[1])->find();
            $res['category'] = db('market_trading_cid')->where('pid', 0)->select();
            $jn[0] = "__USER__ u";
            $jn[1] = "u.uid = s.uid";
            $field1 = "s.id,s.store,s.uid,s.time,u.username";

            $time = trim(input('get.time'));
            $store = trim(input('get.store'));
            if ($time == 1 && $store == null) {
                $order = 'time desc';
            } else if ($time == 2 && $store == null) {
                $order = 'time asc';
            } else if ($time == null && $store == 1) {
                $order = 'store desc';
            } else if ($time == null && $store == 2) {
                $order = 'store asc';
            } else if ($time == 1 && $store == 1) {
                $order = 'time desc,store desc';
            } else if ($time == 1 && $store == 2) {
                $order = 'time desc,store asc';
            } else if ($time == 2 && $store == 1) {
                $order = 'time asc,store desc';
            } else if ($time == 2 && $store == 2) {
                $order = 'time asc,store asc';
            } else {
                $order = 'rand()';
            }

            $res['offer'] = db('offer')->where('mid', $res['id'])->where('s.static',1)->order($order)->alias('s')->join($jn[0], $jn[1])->field($field1)->select();
        }

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }
   /**
     * 选择OK
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function confirm()
    {
        $value = trim(input('get.value'));
        $vvalue = trim(input('post.value'));
        $king = trim(input('get.king'));
        $caking = trim(input('post.king'));
        if ($value == 1 || $vvalue == 1) {
            if ($king == 1 || $caking == 1) {
                $id = trim(input('get.id'));
                $uid = trim(input('get.uid'));
                $offer = db('offer')->where('id', $id)->find();
                $caking = db('orders')->where('id',$offer['order_id'])->value('id');
                $data['order_status'] = 2;
                $data['order_num'] = $caking;
                $data['time'] = time();
                $data['order_msg'] = '已选择买家，等待买家付款';
                $data1['status'] = 2;
                $data1['s_time'] = strtotime("+14 days");
                $data1['offer_id'] = $offer['id'];
                $data1['username'] = $offer['uid'];
                $data1['money'] = $offer['store'];
                $data1['statuc'] = 2;
                $data2['static'] = 2;
                $data3['static'] = 3;
                $standard = 1;
                $res = db('orders')->where('id',$offer['order_id'])->find();
                if ($res['status'] == 1) {
                    $data2['order_id'] = $res['id'];

                    //启动事务
                    Db::startTrans();
                    try {
                        Db::name('order_msg')->insert($data);
                        Db::name('offer')->where('id', $id)->update($data2);
                        Db::name('orders')->where('id',$offer['order_id'])->update($data1);
                        Db::name('offer')->where('mid', $uid)->where('static',1)->where('id', "<>", $offer['id'])->update($data3);
                        Db::commit();
                        $standard = 2;
                    } catch (\Exception $e) {
                        Db::rollback();
                    }
                    if ($standard == 2) {
                        action_log_index('店铺中心选择买家', 'log_ment', 1);
                        $this->_jsonp('200', '提交成功', null);
                    } else {
                        action_log_index('店铺中心选择买家', 'log_ment');
                        $this->_jsonp('1000', '提交失败', null);
                    }
                } else {
                    $this->_jsonp('1000', '提交失败', null);
                }
            } else {

                $id = Request::instance()->only('id', 'post');
                $where['name'] = trim(input('post.uid'));
                if (!isset($id['id'])) {
                    $this->json('1000', '请选择', null);
                }
                $count = count($id['id']);
				$uid = trim(input('post.user'));
                $market = db('market_trading')->where('id', $where['name'])->find();
                $where1['name'] = $market['id'];
                $where1['services'] = 1;
                $where1['status'] = array('in', array(2, 3, 4, 5, 6, 7, 8));
                $order = db('orders')->where($where1)->count();

                if ($count + $market['value'] + $order > $market['volist']) {
                    $this->json('1000', '您的选择超过了这个商品的最高授权', null);
                } else {
                    foreach ($id['id'] as $key => $value) {
						$res['data'][$key] = db('offer')->where('id', $value)->find();
                        $order_num = db('orders')->where('id',$res['data'][$key]['order_id'])->value('id');
                        $orders = db('orders')->where('id', $res['data'][$key]['order_id'])->find();
                        if ($orders['offer_id'] == null) {
                            $data['offer_id'] = $res['data'][$key]['id'];
                            $data['username'] = $res['data'][$key]['uid'];
                            $data['status'] = 2;
                            $data['statuc'] = 2;
                            $data['s_time'] = strtotime("+14 days");
                            $data['money'] = $res['data'][$key]['store'];

                            $data1['order_status'] = 2;
                            $data1['order_num'] = $order_num;
                            $data1['time'] = time();
                            $data1['order_msg'] = '已选择买家，等待买家付款';

                            $user = db('user')->where('uid', $orders['seller'])->find();
                            $data['alipay'] = $user['alipay'];

                            db('order_msg')->insert($data1);
                            db('orders')->where('id',$res['data'][$key]['order_id'])->update($data);
                            $order = db('orders')->where('offer_id', $res['data'][$key]['id'])->find();
                            $order_id['order_id'] = $order['id'];
                            $order_id['static'] = 2;
                            db('offer')->where('id', $order['offer_id'])->update($order_id);
                        } else {
                            if ($orders['offer_id'] != $res['data'][$key]['id']) {
                                $requery['offer_id'] = $res['data'][$key]['id'];
                                $requery['username'] = $res['data'][$key]['uid'];
                                $requery['status'] = 2;
                                $requery['statuc'] = 2;
                                $requery['s_time'] = strtotime("+14 days");
                                $requery['money'] = $res['data'][$key]['store'];
                                $requery['name'] = $res['data'][$key]['mid'];
                                $requery['seller'] = $uid;
                                $form = new Form();
                                $requery['order'] = $form->rand_order();
                                $requery['time'] = time();
                                $requery['services'] = 1;

                                $user = db('user')->where('uid', $uid)->find();
                                $requery['alipay'] = $user['alipay'];

                                $data2['order_id'] = db('orders')->insertGetId($requery);
                                $data2['static'] = 2;

                                $data3['order_status'] = 2;
                                $data3['order_num'] = $data2['order_id'];
                                $data3['time'] = time();
                                $data3['order_msg'] = '已选择买家，等待买家付款';
                                db('offer')->where('id', $res['data'][$key]['id'])->update($data2);
                                db('order_msg')->insert($data3);
                                action_log_index('店铺中心选择买家', 'log_ment', 1);
                            } else {
                                action_log_index('店铺中心选择买家', 'log_ment');
                                $this->json('1000', '您已选中', null);
                            }
                        }
                    }
                    $data4['static'] = 3;
                    db('offer')->where('mid', $where['name'])->where('static', 1)->update($data4);
                    action_log_index('店铺中心选择买家', 'log_ment', 1);
                    $this->json('200', '您已选中', null);
                }
            }
        }

    }

    /**
     * 上传专利号
     */
    public function upload_file()
    {
        $id = trim(input('post.id'));//订单id
        if (implode('', $_FILES['upfile']['name'])) {
            $show = array();
            //oss上传
//            $ossClient = Oss::getInstance();
//            $bucket = Oss::getBucketName();
//            foreach($_FILES['upfile']['name'] as $key=>$v){
//               if($v){
//                   $ext = substr($v, stripos($v, '.'));
//                   $rand = md5(time() . rand(100, 999));
//                   $object = "contract/" . $rand . $ext;
//                   $content = $_FILES['upfile']['tmp_name'][$key];
//                   $result = $ossClient->uploadFile($bucket, $object, $content);
//                   $show[]=$result['oss-request-url'];
//               }
//            }
            //本地上传
            $files = request()->file('upfile');
            $news_list = new news_list();
            foreach ($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $news_list->upload($file, 'Images/contract');
                $show[] = $info;
            }
            // echo json_encode($show);die;
            if ($show) {
                $where['id'] = $id;
                $res = db('orders')->where($where)->find();
                if ($res['index'] >= 3) {
                    $this->json('1000', '抱歉,您已上传最大次数', null);
                    return false;
                } else {
					db('orders')->where($where)->setInc('index');
                    $data['order_status'] = '4';//上传合同
                    $data['order_num'] = $id;
                    $data['order_msg'] = json_encode($show);
                    $data['time'] = time();
                    $data1['status'] = '4';
                    $data1['statuc'] = '4';
                    $data1['contract'] = json_encode($show);
                    $data1['s_time'] = strtotime("+3 days");
                    $standard = 1;
                    if ($res['status'] == 3) {
						if($res['index'] > 1){
							$where2['order_status'] = 5;
							$where2['order_num'] = $id;
							$res['order_msg'] = db('order_msg')->where($where2)->select();
							foreach($res['order_msg'] as $value){
								db('order_msg')->where('id',$value['id'])->delete();
							}
						}
                        //启动事务
                        Db::startTrans();
                        try {
                            Db::name('order_msg')->insert($data);
                            Db::name('orders')->where($where)->update($data1);
                            Db::commit();
                            $standard = 2;
                        } catch (\Exception $e) {
                            Db::rollback();
                        }
                        if ($standard == 2) {
                            action_log_index('店铺中心上传专利号', 'log_ment', 1);
                            $this->json('200', '提交成功', null);
                        } else {
                            action_log_index('店铺中心上传专利号', 'log_ment');
                            $this->json('1000', '提交失败', null);
                        }
                    } else {
                        $this->_jsonp('1000', '提交失败', null);
                    }
                }
            } else {
                $this->json('1000', '上传失败', null);
            }
        } else {
            $this->json('1000', '未上传图片', null);
        }
    }

    /**
     * 上传快递单号
     */
    public function update_caking()
    {
        $where['id'] = trim(input('get.id'));
        $data1['seller_e'] = trim(input('get.seller_e'));
        $data['order_status'] = '6';//上传合同
        $data['order_num'] = $where['id'];
        $data['time'] = time();
        $data['order_msg'] = '上传快递单号成功';
        $data1['status'] = '6';
        $data1['statuc'] = '7';
        $data1['s_time'] = strtotime("+3 days");
        $standard = 1;
        $res = db('orders')->where($where)->find();
        if ($res['status'] == 5) {
            //启动事务
            Db::startTrans();
            try {
                Db::name('orders')->where($where)->update($data1);
                Db::name('order_msg')->insert($data);
                Db::commit();
                $standard = 2;
            } catch (\Exception $e) {
                Db::rollback();
            }
            if ($standard == 2) {
                action_log_index('店铺中心上传快递单号', 'log_ment', 1);
                $this->_jsonp('200', '提交成功', null);
            } else {
                action_log_index('店铺中心上传快递单号', 'log_ment');
                $this->_jsonp('1000', '提交失败', null);
            }
        } else {
            $this->_jsonp('1000', '提交失败', null);
        }
    }

    /**
     * 确认收款页面
     */
    public function confirmation_receivables_get()
    {
        $where['a.id'] = trim(input('get.id'));
        $field = "a.seller_alipay,m.image,m.username,c.trading_name,m.store,u.alipay";
        $join[0] = "__MARKET_TRADING__ m";
        $join[1] = "m.id = a.name";
        $jo[0] = "__MARKET_TRADING_CID__ c";
        $jo[1] = "m.c_id = c.id";
		$join1[0] = "__USER__ u";
		$join1[1] = "u.uid = a.seller";
        $res = db('orders')->alias('a')->field($field)->join($join[0], $join[1])->join($jo[0], $jo[1])->join($join1[0],$join1[1])->where($where)->find();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 收款
     */
    public function confirmation_receivables_post()
    {
        $where['id'] = trim(input('get.id'));
//        $data['seller_e'] = trim(input('get.seller_e'));
//        $data['status'] = 6;
//        $data['s_time'] = strtotime("+3 days");
//        db('orders')->where($where)->update($data);
        $data['order_status'] = '9';
        $data['order_num'] = $where['id'];
        $data['time'] = time();
        $data['order_msg'] = '商家收款,订单完成';
        $data1['status'] = '9';

        $standard = 1;
//        $join[0] = "__MARKET_TRADING__ m";
//        $join[1] = "m.id = a.name";
//        $join1[0] = "__USER__ u";
//        $join1[1] = "u.uid = a.username";
//        $join2[0] = "__USER__ s";
//        $join2[1] = "s.uid = a.seller";
//        $field = "a.order,a.money,m.image,m.username as name,u.username,s.username as seller";
        $res = db('orders')->where($where)->find();
        $trading = db('market_trading')->where('id', $res['name'])->find();
        if ($res['status'] == 8) {
//            $data2['order'] = $res['order'];
//            $data2['store'] = $res['money'];
//            $data2['image'] = $res['image'];
//            $data2['name'] = $res['name'];
//            $data2['username'] = $res['username'];
//            $data2['seller'] = $res['seller'];
//            $data2['time'] = time();
            //启动事务
            Db::startTrans();
            try {
                Db::name('order_msg')->insert($data);
                Db::name('orders')->where($where)->update($data1);
                Db::name('market_trading')->where('id', $res['name'])->setInc('value');
                Db::name('shop_information')->where('id', $trading['name'])->setInc('shop_volume');
                Db::commit();
                $standard = 2;
            } catch (\Exception $e) {
                Db::rollback();
            }
            if ($trading['value'] + 1 >= $trading['volist']) {
                $data2['status'] = 2;
				$data2['static'] = 2;
                db('market_trading')->where('id', $trading['id'])->update($data2);
            }
			$data3['static'] = 4;
            $where1['id'] = $res['offer_id'];
            db('offer')->where($where1)->update($data3);
            if ($standard == 2) {
                action_log_index('店铺中心商家确认收款', 'log_ment', 1);
                $this->_jsonp('200', '提交成功', null);
            } else {
                action_log_index('店铺中心商家确认收款', 'log_ment');
                $this->_jsonp('1000', '提交失败', null);
            }
        } else {
            $this->_jsonp('1000', '收款失败', null);
        }
    }

    /**
     * 订单详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order_details()
    {

        $services = trim(input('get.type'));//type值1表示是trading；2是service
        $order_list = new order_list('orders');
        if ($services == 2) {
            $id = trim(input('get.id'));//訂單id
            $where['w.id'] = $id;
            $type1 = 'zgh_service_market m';
            $condition1 = 'w.s_id=m.id';
            $type2 = 'zgh_service_type t';
            $condition2 = 'm.type=t.id';
            $type3 = 'zgh_shop_information i';
            $condition3 = 'w.seller=i.id';
//            $fields = 'w.*,m.goods_name,m.pic,t.t_name,i.name as shop_name';
            $fields = 'w.services,w.id,w.status as status,w.order,w.time,w.image,m.goods_name as username,w.money as store,t.t_name as trading_name,i.name as market_name,i.name';
            $data = $order_list->order_details($type1, $type2, $type3, $where, $condition1, $condition2, $condition3, $fields);
            $list_name = $order_list->getSeller();
            foreach ($list_name as $k => $v) {
                if ($data['status'] == $k) {
                    $data['list_name'] = $v;
                }
            }
            $data['count1'] = db('order_msg')->order('time desc')->where('type', 2)->where('order_status', 3)->where('order_num', $data['id'])->value('time');
            $data['count2'] = db('order_msg')->order('time desc')->where('type', 1)->where('order_status', 5)->where('order_num', $data['id'])->value('time');
            $data['count4'] = db('order_msg')->order('time desc')->where('type', 2)->where('order_status', 7)->where('order_num', $data['id'])->value('time');
            $data['count5'] = db('order_msg')->order('time desc')->where('type', 2)->where('order_status', 11)->where('order_num', $data['id'])->value('time');


        } else {
            $where1['a.id'] = trim(input('get.id'));
            $join[0] = "__MARKET_TRADING__ m";
            $join[1] = "m.id = a.name";
            $jo[0] = "__MARKET_TRADING_CID__ c";
            $jo[1] = "m.c_id = c.id";
            $in[0] = "__MARKET_TRADING_CATEGORY__ t";
            $in[1] = "m.category_id = t.id";
            $ji[0] = "__SHOP_INFORMATION__ s";
            $ji[1] = "m.name = s.id";
            $field = "a.services,a.id,a.status,a.index,a.money,a.order,a.time,m.image,m.username,m.store,c.trading_name,t.market_name,s.name";
            $data = db('orders')->alias('a')->field($field)
                ->join($join[0], $join[1])->join($jo[0], $jo[1])->join($in[0], $in[1])->join($ji[0], $ji[1])->where($where1)->find();
            $data['count1'] = db('order_msg')->order('time desc')->where('order_status', 2)->where('order_num', $data['id'])->value('time');
            $data['count2'] = db('order_msg')->order('time desc')->where('order_status', 3)->where('order_num', $data['id'])->value('time');
            $data['count3'] = db('order_msg')->order('time desc')->where('order_status', 5)->where('order_num', $data['id'])->value('time');
            $data['count4'] = db('order_msg')->order('time desc')->where('order_status', 6)->where('order_num', $data['id'])->value('time');
            $data['count5'] = db('order_msg')->order('time desc')->where('order_status', 9)->where('order_num', $data['id'])->value('time');

        }
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '评论列表数据获取成功', $data);
        } else {
            $this->_jsonp('1000', '评论列表数据获取失败', null);
        }
//        $where['a.id'] = trim(input('get.id'));
//        $join[0] = "__MARKET_TRADING__ m";
//        $join[1] = "m.id = a.name";
//        $jo[0] = "__MARKET_TRADING_CID__ c";
//        $jo[1] = "m.c_id = c.id";
//        $in[0] = "__MARKET_TRADING_CATEGORY__ t";
//        $in[1] = "m.category_id = t.id";
//        $ji[0] = "__SHOP_INFORMATION__ s";
//        $ji[1] = "m.name = s.id";
//        $field = "a.id,a.status,a.order,a.time,m.image,m.username,m.store,c.trading_name,t.market_name,s.name";
//        $res = db('orders')->alias('a')->field($field)
//            ->join($join[0], $join[1])->join($jo[0], $jo[1])->join($in[0], $in[1])->join($ji[0], $ji[1])->where($where)->find();
//        $res['count1'] = db('order_msg')->order('time desc')->where('order_status', 2)->where('order_num', $res['id'])->value('time');
//        $res['count2'] = db('order_msg')->order('time desc')->where('order_status', 3)->where('order_num', $res['id'])->value('time');
//        $res['count3'] = db('order_msg')->order('time desc')->where('order_status', 5)->where('order_num', $res['id'])->value('time');
//        $res['count4'] = db('order_msg')->order('time desc')->where('order_status', 6)->where('order_num', $res['id'])->value('time');
//        $res['count5'] = db('order_msg')->order('time desc')->where('order_status', 9)->where('order_num', $res['id'])->value('time');
//        if (isset($res) || !empty($res)) {
//            $this->_jsonp('200', '查询成功', $res);
//        } else {
//            $this->_jsonp('1000', '查询失败', null);
//        }
    }

    /**
     * 个人认证和企业认证
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function store_open()
    {
        $keyword = trim(input('get.keyword'));
        if ($keyword == 'enterprise') {
            $where['uid'] = trim(input('get.uid'));
            $res['shop'] = db('authentication_enterprise')->where($where)->find();
            $res['user'] = db('user')->where($where)->find();
        } else {
            $where['uid'] = trim(input('get.uid'));
            $res['shop'] = db('authentication_personal')->where($where)->find();
            $res['user'] = db('user')->where($where)->find();
        }
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '认证', $res);
        } else {
            $this->_jsonp('1000', '未认证', null);
        }
    }

    /**
     * 添加店铺
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function store_form()
    {
        $where['uid'] = trim(input('get.uid'));
        $user = db('user')->where($where)->find();
        $res = db('shop_information')->where($where)->find();
        if ($res == false) {
            $data['username'] = $user['username'];
            $data['uid'] = $user['uid'];
            $data['phone'] = $user['phone'];
            $data['email'] = $user['email'];
            $data['time'] = time();
            $data['shop_id'] = trim(input('get.keyword'));

            $res = db('shop_information')->where($where)->insert($data);
        } else {
            if ($res['status'] == 3) {
                $where1['id'] = $res['id'];
                $data1['status'] = 1;
                db('shop_information')->where($where)->update($data1);
            }
        }
        if ($res == true) {
            action_log_index('店铺中心新增店铺', 'log_ment', 1);
            $this->_jsonp('200', '新增成功', null);
        } else {
            action_log_index('店铺中心新增店铺', 'log_ment');
            $this->_jsonp("1000", '添加失败', null);
        }
    }

    /**
     * 查询验证成功没
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function store_open3()
    {
        $where['uid'] = trim(input('get.uid'));
        $res = db('shop_information')->where($where)->find();
		
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '认证', $res);
        } else {
            $this->_jsonp('1000', '未认证', null);
        }
    }

    /**
     * 获取
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_open6()
    {
        $where['uid'] = trim(input('get.uid'));
        $res = db('shop_information')->where($where)->find();
        $res['label_shop'] = db('shop_label')->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询', $res);
        } else {
            $this->_jsonp('1000', '未查询', null);
        }
    }

	public function get_open7(){
		
        $where['uid'] = trim(input('get.uid'));
        $res = db('shop_information')->where($where)->find();
		
		if($res['static'] == 1){
            $this->_jsonp('1001', '已通过', null);
		}
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '认证', $res);
        } else {
            $this->_jsonp('1000', '未认证', null);
        }
	}
	
    /**
     * 修改个人店铺
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function store_open6()
    {
        $news_list = new news_list();
        $files = request()->file('img');
        $image = trim(input('post.image'));
        if (!empty($files)) {
			 // 
            $match = new Preg();
            $token=Cookie::get('access_token','think_');
            if(empty($token)){
                $info= $match->get_tooken();
                $arrs=json_decode($info, true);
                $token=$arrs["access_token"];
                Cookie::set('access_token',$token,1728000);//使用think\Cookie
            }
            $res=$match->get_img($token,$_FILES['img']['tmp_name']);
            $results=json_decode($res, true);
            if($results['conclusion']!='合规'){
                $this->json('1000', '图片不合规范', null);
            }
            //
            $img = $news_list->upload($files, 'Images/Information');
            $data['image'] = $img;
        } else if (!empty($image)) {
            $data['image'] = $image;
        } else {
            $data['image'] = "store.jpg";
        }
        $where['id'] = trim(input('post.id'));

        $data['name'] = trim(input('post.name'));
        $data['scope'] = trim(input('post.scope'));
        $data['tenet'] = trim(input('post.tenet'));
        $data['label'] = trim(input('post.label'));

    
		$shop_info = db('shop_information')->where('id', '<>', $where['id'])->select();
		foreach ($shop_info as $key => $value) {
			if ($value['name'] == $data['name']) {
				$this->json('1000', '店铺名称已存在', null);
				break;
			}
		}
		$res = db('shop_information')->where($where)->update($data);
        
        if (isset($res) || !empty($res)) {
            action_log_index('店铺中心修改个人店铺', 'log_ment', 1);
            $this->json('200', '修改成功', $res);
        } else {
            action_log_index('店铺中心修改个人店铺', 'log_ment');
            $this->json('1000', '修改失败', null);
        }
    }

    /**
     * 我的消息主页
     */
    public function message()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $where['to_uid'] = trim(input('get.uid'));
        $where['reply_type'] = 2;
        $where['index'] = 1;
        $join[0] = "__USER__ u";
        $join[1] = "u.uid = a.from_uid";
        $field = "a.id,a.content,a.time,a.status,a.pid,a.from_uid,u.username";
        $where1['a.status'] = array('in', array(1,2));
        $data = db('goods_chat')->where($where)->order('status desc')->order('time desc')->alias('a')->join($join[0], $join[1])->where($where1)->limit(10)->page($page)->field($field)->select();
        //屏蔽关键字
		$match = new Preg();
		foreach($data as $key=>$val){
                $html=$match->replace($val['content']);
				$data[$key]['content']=$html;
            }
			//
			$res['data']=$data;
		$res['count'] = db('goods_chat')->where($where)->count();
//        foreach ($res['data'] as $key => $value) {
//            $res['data'][$key] = db('goods_chat')->where('id', $value['pid'])->where($where)->order('time desc')->alias('a')->field($field)->join($join[0], $join[1])->find();
//            if($res['data'][$key] == null){
//                $res['data'][$key] = $value;
//            }
//        }

        $Page = new \app\api\model\Page($res['count'], 10);
        $render = $Page->render;

        $res['render'] = $render;

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 更改状态
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del_all()
    {
        $id = trim(input('get.uid'));
        $where['id'] = explode(",", $id);
        $data['status'] = trim(input('get.status'));
        for ($i = 0; $i < count($where['id']); $i++) {
            $res = db('goods_chat')->where('id', $where['id'][$i])->update($data);
        }
        if ($res == true) {
            action_log_index('店铺中心删除消息', 'log_ment', 1);
            $this->_jsonp('200', '更新成功', $res);
        } else {
            action_log_index('店铺中心删除消息', 'log_ment');
            $this->_jsonp('1000', '更新失败', null);
        }
    }

    /**
     * 查询消息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_mes()
    {
        $where['id'] = trim(input('get.id'));
        $res = db('goods_chat')->where($where)->find();
        $where1['status'] = array('in', array(1,2));
        $order = "time asc";
        $where1['from_uid'] = $res['from_uid'];
        $where1['to_uid'] = $res['to_uid'];
        $res['data'] = db('goods_chat')->order($order)->where($where1)->select();
        foreach ($res['data'] as $key => $value) {
            $res['data'][$key]['tel'] = db('goods_chat')->order($order)->where('pid', $value['id'])->select();
        }

        if (isset($res) || !empty($res)) {
            $data['status'] = 1;
            db('goods_chat')->where($where)->update($data);
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }


    /**
     * 回复
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check()
    {
        $where['id'] = trim(input('get.id'));
        $king = db('goods_chat')->where($where)->find();
        $data['reply_type'] = 1;
        $data['content'] = trim(input('get.content'));
        $data['from_uid'] = $king['to_uid'];
        $data['to_uid'] = $king['from_uid'];
        $data['pid'] = $king['id'];
        $data['time'] = time();
		$data['index'] = 1;

        $where1['to_uid'] = $king['from_uid'];
        $where1['from_uid'] = $king['to_uid'];
        $where1['reply_type'] = 1;
        $data1['index'] = 0;
        db('goods_chat')->where($where1)->update($data1);
        $res = db('goods_chat')->insert($data);
        if ($res == true) {
            action_log_index('店铺中心回复买家消息', 'log_ment', 1);
            $this->_jsonp('200', '更新成功', $res);
        } else {
            action_log_index('店铺中心回复买家消息', 'log_ment');
            $this->_jsonp('1000', '更新失败', null);
        }
    }

    /**
     * 申诉列表
     */
    public function complain()
    {
        $where['uid'] = trim(input('get.uid'));
        $where['status'] = 2;
        $where['static'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
		if($where['static'] == 3){
			$where['static'] = array('in',array(3,4));
		}
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
         $data= db('complain')->where($where)->limit(10)->page($page)->select();
		//屏蔽关键字
		$match = new Preg();
		foreach($data as $key=>$val){
                $html=$match->replace($val['problem']);
				$data[$key]['problem']=$html;
            }
			//
			$res['data']=$data;
        $res['num'] = db('complain')->where($where)->field('static,count(id) number')->group('static')->select();
        $where1['static'] = 1;
		$where1['status'] = 2;
		$where1['uid'] = $where['uid'];
		$where2['static'] = 2;
		$where2['status'] = 2;
		$where2['uid'] = $where['uid'];
		$where3['static'] = array('in',array(3,4));
		$where3['status'] = 2;
		$where3['uid'] = $where['uid'];
		$where4['uid'] = $where['uid'];
		$where4['status'] = 2;
		$where4['static'] = array('neq',0);
        $res['count0'] = db('complain')->where($where4)->count();
        $res['count1'] = db('complain')->where($where1)->count();
        $res['count2'] = db('complain')->where($where2)->count();
        $res['count3'] = db('complain')->where($where3)->count();
		
		if($where['static'] == 1){
			$Page = new \app\api\model\Page($res['count1'], 10);
			$res['count'] = $res['count1'];
		}else if($where['static'] == 2){
			$res['count'] = $res['count2'];
			$Page = new \app\api\model\Page($res['count2'], 10);
		}else if($where['static'] == 3){
			$res['count'] = $res['count3'];
			$Page = new \app\api\model\Page($res['count3'], 10);
		}else{
			$res['count'] = $res['count0'];
			$Page = new \app\api\model\Page($res['count0'], 10);
		}
        $render = $Page->render;

        $res['render'] = $render;

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 申诉详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function details()
    {
        $where['id'] = trim(input('get.id'));
        $res = db('complain')->where($where)->find();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 服务分类
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function category_service()
    {
        $id = trim(input('get.id'));
        $res = db('service_type')->where('pid', $id)->select();
        $this->_jsonp('200', 'OK', $res);
    }

    /**
     * 收藏首页
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info_list()
    {
        $where['c.uid'] = trim(input('get.uid'));
        $where['c.static'] = 0;
        $where['c.type'] = trim(input('get.all'));
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 3;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;

        $join[0] = "__COLLECT__ c";
        $join[1] = "c.mid = a.id";
        $filed = "a.id,a.shop_id,a.image,a.name,a.scope,a.username,a.label,c.time";
        $res['data'] = db('shop_information')->alias('a')->join($join[0], $join[1])->limit($number)->page($page)->field($filed)->where($where)->select();
        $res['count'] = db('shop_information')->alias('a')->join($join[0], $join[1])->where($where)->count();
        $res['label'] = db('shop_label')->select();
        //分页
        $Page = new \app\api\model\Page($res['count'], $number);
        $render = $Page->render;
        $res['render'] = $render;
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '店铺收藏列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '店铺收藏数据获取失败', null);
        }
    }

    /**
     * 取消收藏
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function info_del()
    {
        $id = trim(input('get.id'));
        $where['id'] = explode(",", $id);
        for ($i = 0; $i < count($where['id']); $i++) {
            $res = db('collect')->where('mid', $where['id'][$i])->delete();
        }
        if (isset($res) || !empty($res)) {
			action_log_index('取消店铺收藏', 'log_ment', 1);
            $this->_jsonp('200', '取消店铺收藏成功', $res);
        } else {
			action_log_index('取消店铺收藏', 'log_ment');
            $this->_jsonp('1000', '取消店铺收藏失败', null);
        }
    }

    /**
     * 验证店铺 图片和名称
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cation()
    {
        $where['uid'] = trim(input('get.uid'));
        $where['static'] = 1;
        $where['status'] = 2;
        $field = "image,name";
        $res = db('shop_information')->field($field)->where($where)->find();
        if (isset($res) || !empty($res)) {
            if ($res['image'] == null || $res['name'] == null) {
                $this->_jsonp('1001', '资料未完善', null);
            } else {
                $this->_jsonp('200', "幸运", $res);
            }
        } else {
            $this->_jsonp('1000', '您还没有开通店铺', null);
        }
    }

	/**
	*反馈分类查询
	**/
	public function get_category_problem(){
		$res = db('category_problem')->select();
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '反馈分类查询成功', $res);
        } else {
            $this->_jsonp('1000', '反馈分类查询失败', null);
        }
	}
/**
     * 定时执行  时间过期
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function timing()
    {
        //订单代付款超时
        $where1['status|statuc'] = 2;
        $res['orders'] = db('orders')->where($where1)->where('s_time', '<=', time())->select();
        foreach ($res['orders'] as $v) {
            $data1['status'] = 10;
            $data1['statuc'] = 10;
            if ($v['status'] == 2 || $v['statuc'] == 2) {
                db('orders')->where('id', $v['id'])->update($data1);
            }

        }

        //订单代选择超时
        $where2['status|statuc'] = 1;
        $res['order'] = db('orders')->where($where2)->where('s_time', '<=', time())->select();
        foreach ($res['order'] as $v) {
            $data2['status'] = 11;
            $data2['statuc'] = 11;
            if ($v['statuc'] == 1 || $v['status'] == 1) {
                db('orders')->where('id', $v['id'])->update($data2);
            }
        }

        //商品过期时间
        $res['market_trading'] = db('market_trading')->where('status', 1)->where('be_time', '<=', time())->select();
        foreach ($res['market_trading'] as $v) {
            $data3['status'] = 2;
            db('market_trading')->where('id', $v['id'])->update($data3);
        }

        //清除时间超过7天的日志
        $time = strtotime('-7 days');
        $res['mysql'] = db('log_visitor')->where('time', '<=', $time)->select();
        foreach ($res['mysql'] as $v) {
            db('log_visitor')->where('id', $v['id'])->delete();
        }

        //身份证有效期截止前三天自动下架店铺 企业认证中心
        $mytime = date("Y-m-d", strtotime("-3 day"));
        $where3['static'] = 2;
        $field = 'validity_time,legal_date,id,uid,phone';
        $res['authentication_enterprise'] = db('authentication_enterprise')->field($field)->where($where3)->select();
        foreach ($res['authentication_enterprise'] as $v) {
            $data4['static'] = 3;
            $data4['result'] = '您的认证信息过期，请重新认证！';

            $data5['static'] = 2;
            $data5['result'] = '您的认证信息过期，请重新认证！';
            if ($v['validity_time'] <= $mytime || $v['legal_date'] <= $mytime) {
                db('authentication_enterprise')->where('id', $v['id'])->update($data4);
                db('shop_information')->where('uid', $v['uid'])->update($data5);
                $data6['title'] = '信息过期';
                $data6['content'] = '您的认证信息过期，请重新认证！';
                $data6['uid'] = $v['uid'];
                $data6['status'] = 1;
                $data6['time'] = time();
                db('platform')->insert($data6);
//                $this->sms_phone($v['phone']);
            }
        }

        //身份证有效期截止前三天自动下架店铺 个人认证中心
        $field1 = 'id,uid,phone';
        $res['authentication_personal'] = db('authentication_personal')->field($field1)->where('static', 2)->where('validity_time', '<=', $mytime)->select();
        foreach ($res['authentication_personal'] as $v) {
            $data7['static'] = 3;
            $data7['result'] = '您的认证信息过期，请重新认证！';

            $data8['static'] = 2;
            $data8['result'] = '您的认证信息过期，请重新认证！';
            db('authentication_personal')->where('id', $v['id'])->update($data7);
            db('shop_information')->where('uid', $v['uid'])->update($data8);

            $data9['title'] = '信息过期';
            $data9['content'] = '您的认证信息过期，请重新认证！';
            $data9['uid'] = $v['uid'];
            $data9['status'] = 1;
            $data9['time'] = time();
            db('platform')->insert($data9);
//            $this->sms_phone($v['phone']);
        }
    }


    /**
     * @param $Mobile
     * @return mixed
     */
    public function sms_phone($Mobile)
    {
        $data = "id=%s&pwd=%s&to=%s&Content=%s&time=";
        $id = "opticsipr";
        $pwd = 'Lanbroad202';
        $url = "http://service.winic.org:8009/sys_port/gateway/index.asp?";
        $to = $Mobile;
        $Content = "尊敬的客户您好,您的认证信息过期，请重新认证。如非本人操作，可不用理会！【知果汇】";
        $content = urlencode(iconv("UTF-8", "GB2312", $Content));
        $rdata = sprintf($data, $id, $pwd, $to, $content);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $rdata);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
        $result = explode("/", $result);
    }
	
}


